<?php

namespace app\common\service;

use think\Exception;
use think\Loader;
use think\Log;

Loader::import('WxPay.WxPay', EXTEND_PATH, '.Api.php');

class WxNotify extends \WxPayNotify {
    public function NotifyProcess($data, &$msg) {
        if ($data['result_code'] == 'SUCCESS') {
            $orderNo = $data['out_trade_no'];
            try {
                $order = model('Order')->get(['order_no' => $orderNo]);
                if ($order->status != config('code.paid')) {
                    $order->status = config('code.paid');
                    $order->save();
                    $products = $order->products;
                    if (!empty($products['head'])) {
                        $head = [
                            'position_id'   => 2,
                            'position_name' => '首页中部',
                            'title'         => $products['head']['head_title'],
                            'belong_id'     => $order->information_id,
                            'cat_id'        => $products['head']['cat_id'],
                            'cat_name'      => $products['head']['cat_name'],
                            'status'        => config('code.status_show'),
                            'expert'        => time() + $products['head']['count'] * 24 * 60 * 60
                        ];
                        model('Banner')->add($head);
                    }
                    if (!empty($products['top'])) {
                        model('Information')->where(['id' => $order->information_id])->update([
                            'is_top'     => 1,
                            'top_expert' => time() + $products['top']['count'] * 24 * 60 * 60
                        ]);
                    }
                    model('Information')->where(['id' => $order->information_id])->update(['need_pay' => 0]);
                }
            } catch (Exception $e) {
                Log::write($e, 'error');
                return false;
            }
            return true;
        } else {
            return false;
        }
    }
}